package com.gsn.poker.play;

import java.util.List;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.gsn.desktop.engine.ActorUtility;
import com.gsn.engine.gdx.GsnParticleActor;
import com.gsn.engine.myplay.GsnGame;
import com.gsn.poker.asset.PokerTexture;

public class ViewerListGroup extends Group {
	Group textGroup = new Group();
	public BoardLayer parent;
	List<String> listName;	
	List<Boolean> listState;
	List<Integer> listID;

	public ViewerListGroup(BoardLayer _parent) {
		transform = GsnGame.transform;
		Image bg = new Image(PokerTexture.inst().avatarBG);
		bg.height = 10.5f * PokerTexture.inst().fontMedium.getLineHeight();
		// bg.width ;
		parent = _parent;
		bg.setClickListener(new ClickListener() {

			@Override
			public void click(Actor actor, float x, float y) {
				// TODO Auto-generated method stub
				GsnParticleActor par = new GsnParticleActor(PokerTexture.inst().clickEff);
				Vector2 v = ActorUtility.getCoordinate(actor, x, y, parent.getRoot());
				par.setPostition(v.x, v.y);
				addActor(par);
				par.start();

				remove();
				parent.viewBtn.visible = !parent.btnGroupOne.visible;
				if (parent.myChair < 0) {
					parent.viewing.visible = !parent.btnGroupOne.visible;
					parent.dotView1.visible = parent.viewing.visible;
					parent.dotView2.visible = parent.viewing.visible;
					parent.dotView3.visible = parent.viewing.visible;
				} else {
					parent.viewing.visible = false;
					parent.dotView1.visible = false;
					parent.dotView2.visible = false;
					parent.dotView3.visible = false;
				}
			}
		});

		addActor(bg);
		addActor(textGroup);
		textGroup.height = 9 * PokerTexture.inst().fontMedium.getLineHeight();
		textGroup.width = bg.width;

		this.width = bg.width;
		this.height = bg.height;
	}

	public void update(List<Integer> listID, List<String> listName, List<Boolean> listState) {
		this.listID = listID;
		this.listName = listName;
		this.listState = listState;
		textGroup.clear();
		for (int i = 0; i < listName.size(); i++) {

			Label name = null;
			if (listState.get(i)) {
				Label succ = new Label("Tham gia", new Label.LabelStyle(PokerTexture.inst().fontMedium, new Color(1, 1, 0, 1)));
				succ.y = (9 - i - 0.5f) * PokerTexture.inst().fontMedium.getLineHeight();
				succ.x = textGroup.width - succ.width - 5;
				textGroup.addActor(succ);
				name = new Label(ActorUtility.getShortDefineName(listName.get(i), 8), new Label.LabelStyle(PokerTexture.inst().fontMedium, new Color(1, 1, 1, 1)));

				textGroup.addActor(name);
			} else {
				name = new Label(ActorUtility.getShortDefineName(listName.get(i), 15), new Label.LabelStyle(PokerTexture.inst().fontMedium, new Color(1, 1, 1, 1)));

				textGroup.addActor(name);
			}
			name.x = 5;
			name.y = (9 - i - 0.5f) * PokerTexture.inst().fontMedium.getLineHeight();
		}
	}

	public void add(int id, String name, boolean state) {
		listName.add(name);
		listState.add(state);
		listID.add(id);
		update(listID, listName, listState);
	}

	public void remove(String name) {
		for (int i = 0; i < listName.size(); i++)
			if (name.equals(listName.get(i))){
				listID.remove(i);
				listName.remove(i);
				listState.remove(i);
				update(listID, listName, listState);
				break;
			}			
	}

	public void remove(int id) {
		for (int i = 0; i < listName.size(); i++)
			if (id == listID.get(i)){
				listID.remove(i);
				listName.remove(i);
				listState.remove(i);
				update(listID, listName, listState);
				break;
			}			
		
	}
}
